﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ContestMeter.Common;
using System.Windows.Input;
using System.Collections.ObjectModel;
using Microsoft.Practices.Unity;
using System.Windows;
using Microsoft.Win32;
using System.IO;
using ContestMeter.UI;

namespace ContestMeter.Participants.UI
{
    public class AddSolutionViewModel : ViewModel<AddSolutionView>
    {
        [Dependency]
        public IConfiguration Configuration { private get; set; }
        [Dependency]
        public IFileSystem FileSystem { private get; set; }

        public Participant Participant { private get; set; }

        private Solution _solution;
        public Solution Solution
        {
            get { return _solution; }
            set
            {
                if (_solution != value)
                {
                    _solution = value;
                    OnPropertyChanged("Solution");
                }
            }
        }

        #region Save Command
        private DelegateCommand<object> _saveCommand;
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand<object>(x => Save(), x => CanSave());
                }
                return _saveCommand;
            }
        }
        protected virtual void SaveCanExecuteChanged()
        {
            if (_saveCommand != null)
            {
                _saveCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion

        #region Browse Command
        private DelegateCommand<object> _browseCommand;
        public ICommand BrowseCommand
        {
            get
            {
                if (_browseCommand == null)
                {
                    _browseCommand = new DelegateCommand<object>(x => Browse(), x => !IsBusy);
                }
                return _browseCommand;
            }
        }

        #endregion
        protected virtual void Save()
        {
            if (Solution.DevTool != null && Solution.LocalSourcePath != null && Solution.Task != null)
            {
                if (Participant.Solutions.Count(x => x.Task.Name == Solution.Task.Name) > 0)
                {
                    MessageBox.Show("Решение для этой задачи уже существует. Перед добавлением нового решения удалите существующее.");
                    return;
                }
                Participant.Solutions.Add(Solution);
                Participant.Save(Configuration.SolutionsFolder);
                View.DialogResult = true;
                View.Close();
            }
            else
            {
                MessageBox.Show("Для продолжения работы нужно ввести данные во все обязательные поля.");
            }
        }

        public IEnumerable<DeveloperTool> DevTools
        {
            get { return Configuration.DevTools; }
        }

        public bool CanSave()
        {
            return !IsBusy;   
        }

        public void Browse()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            
            if ( (dialog.ShowDialog() ?? false) && dialog.CheckFileExists)
            {
                string taskName = Path.GetFileNameWithoutExtension(dialog.FileName);
                var task = Configuration.Tasks.FirstOrDefault(x => x.ExecutableName == taskName);
                if (task == null)
                {
                    MessageBox.Show("Название файла не соответствует ни одному заданию");
                    return;
                }
                Solution = new Solution(FileSystem);
                Solution.Task = task;
                Solution.LocalSourcePath = dialog.FileName;
                
            }
        }
    }
}
